import router from '@/router';
import axios from 'axios'
import store from '../store/store'
import {ElMessage} from 'element-plus'

let version = "V1"
let base = `https://localhost:7234/api/${version}`;

axios.defaults.timeout=5000;//5秒超时

//请求拦截
axios.interceptors.request.use(
    (config) => {
        //拦截的业务逻辑
        //获取本地token
        if (sessionStorage.getItem('token')) { 
            store.commit('setToken', sessionStorage.getItem('token')) 
        } 
        //console.log('请求拦截器',store.state.token)
        const token = store.state.token;

        if(token)
        {
            config.headers.Authorization = `Bearer ${token}`
        }
        return config;
    },
    (err)=>{
        //拦截失败
        return Promise.reject(err);
    }
)

//响应拦截
axios.interceptors.response.use(
    (res)=>{
        return res;
    },
    (err)=>{
        //如果401状态码
        if(err.response && err.response.status === 401){

            //console.log('登录已过期，请重新登录！');

            ElMessage.error('登录已过期，请重新登录！');

            //store.commit("user/serUser",{});
            store.commit('setToken', ''); 
            const fullPath = encodeURIComponent(router.currentRoute.value.fullPath);
            //引入router模块
            router.push('/login?redirectUrl=' + fullPath);
        }
        return Promise.reject(err);
    }
)

//#region 角色权限管理

//#region 用户登录
/** 登录 */
export const requestLogin = params => {
    return axios.get(`${base}/login/jwttoken3.0`, {params: params}).then(res => res.data);
};
/**获取验证码图片地址 */
export const getCaptcha=param=>{
    return `${base}/File/Captcha/Captcha?id=${param}`
}
/**验证验证码 */
export const Validate = param=>{
    return axios.get(`${base}/File/Validate/Validate`,{params:param}).then(res=>res.data);
}
//#endregion

//#region 用户账户管理

/**通过Token获取用户信息 */
export const getUserInfoByToken = params=>{
    return axios.get(`${base}/User/GetInfoByToken`,{params:params}).then(res=>res.data);
}
/**
 * 获取用户列表
 * @param {*} params 
 * @returns 
 */
export const getUserListPage = params =>{
    return axios.get(`${base}/User/Get`,{params:params}).then(res=>res.data);
};
/**
 * 修改用户信息
 * @param {*} params 
 * @returns 
 */
export const editUserInfo=params=>{
    return axios.put(`${base}/User/PutUserInfo`,params).then(res=>res.data);
}
/**
 * 新增用户信息
 * @param {*} params 
 * @returns 
 */
export const postUserInfo = params=>{
    return axios.post(`${base}/User/PostUserInfo`,params).then(res=>res.data);
}
/**
 * 删除用户信息
 * @param {*} params 
 * @returns 
 */
export const delUserInfo = params=>{
    return axios.delete(`${base}/User/DelUserInfo`,{params:params}).then(res=>res.data);
}
//#endregion

//#region 菜单管理
/**
 * 获取菜单列表
 * @param {*} params 
 * @returns 
 */
export const getPermissionList=params=>{
    return axios.get(`${base}/Permission/Get`,{params:params}).then(res=>res.data);
}
/**
 * 获取菜单表格树
 * @param {*} params 
 * @returns 
 */
export const getTreeTable = params =>{
    return axios.get(`${base}/Permission/GetTreeTable`,{params:params}).then(res=>res.data);
}
/**
 * 获取权限菜单
 * @param {*} params 
 * @returns 
 */
 export const getNavigationBar = params => {
    return axios.get(`${base}/Permission/GetNavigationBar`, {params: params}).then(res => res.data);
};
/**
 * 获取角色菜单权限
 * @param {*} params 
 * @returns 
 */
export const getRoleNavigationBar = params =>{
    return axios.get(`${base}/Permission/GetRoleNavigationBar`,{params:params}).then(res=>res.data);
}
/**
 * 获取菜单树
 * @param {*} params 
 * @returns 
 */
export const getPermissionTree = params=>{
    return axios.get(`${base}/Permission/GetPermissionTree`,{params:params}).then(res=>res.data);
}
/**
 * 保存菜单权限分配
 * @param {*} params 
 * @returns 
 */
export const putPermission = params=>{
    return axios.put(`${base}/Permission/PutPermission`,params).then(res=>res.data);
}
//#endregion

//#region 部门管理
/**
 * 获取部门树
 * @param {*} params 
 * @returns 
 */
 export const getDepartmentTree = params => {
    return axios.get(`${base}/department/getDepartmentTree`, {params: params}).then(res=>res.data);
};
/**
 * 获取角色列表
 * @param {*} params 
 * @returns 
 */
export const getRoleList = params =>{
    return axios.get(`${base}/Role/Get`,{params:params}).then(res=>res.data);
}
//#endregion

//#endregion